package com.wuba.rxjavademo.lyz.day0416;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.observables.GroupedObservable;

/**
 * Created by liyaxing on 2016/4/16.
 */
public class Demo02 {

    public static void main(String[] ss) throws InterruptedException {

        Demo02 demo02 = new Demo02() ;
//        Observable<List<Integer>> observable =
//                demo02.bufferObservable02() ;
//        observable.subscribe(list -> {
//            print(list);
//        }) ;
//        Observable<String> obserable = demo02.flatMapObservable() ;
//        obserable.subscribe(s -> print(s)) ;
//        Observable<String> observable = demo02.flatMapIterableObservable() ;
//        observable.subscribe(s -> {
//            print(s);
//        }) ;
//        Observable<GroupedObservable<String,Integer>> observable = demo02.groupByObservable() ;
//        observable.subscribe(stringIntegerGroupedObservable -> {
//            String key = stringIntegerGroupedObservable.getKey() ;
//            stringIntegerGroupedObservable.subscribe(integer -> {
//                print(key + " : " + integer);
//            }) ;
//        }) ;
        Observable<Observable<Long>> observable = demo02.windowTimeObservable() ;
        observable.subscribe(o -> {
            o.subscribe(s -> print(s)) ;
        }) ;

        Thread.sleep(10000);

    }

    private Observable<Observable<Long>> windowTimeObservable(){
        return Observable.interval(1,TimeUnit.SECONDS).window(3,TimeUnit.SECONDS) ;
    }

    private Observable<Observable<Integer>> windowObservable(){
        return Observable.just(1,2,3,4,5,6,7,8,9).window(3) ;
    }


    private Observable<Integer> scanObservable(){
        return Observable.just(1,2,3,4,5,6,7,8,9).scan((x,y)-> x + y) ;
    }

    private Observable<Integer> castObservable(){
        return Observable.<Object>just(1,2,3,4,5,6).cast(Integer.class) ;
    }


    private Observable<Integer> mapObservable(){
        return Observable.just(1,2,3,4,5,6).map(integer -> integer * 2) ;
    }

    private Observable<GroupedObservable<String,Integer>> groupByObservable(){
        return Observable.just(1,2,3,4,5,6,7,8).groupBy(integer -> {
            if(integer % 2 == 0){
                return "偶数" ;
            }else {
                return "奇数" ;
            }
        }) ;
    }

    private Observable<String> flatMapIterableObservable(){
        return Observable.just(1,2,3,4,5,6,7).flatMapIterable(integer -> {
            ArrayList<String> list = new ArrayList<>() ;
            for (int i = 0; i < integer; i++) {
                list.add("a" + integer) ;
            }
            return list ;
        }) ;
    }


    private Observable<String> flatMapObservable(){
        return Observable.just(1,2,3,4,5,6).flatMap(integer -> Observable.<String>create(subscriber -> {
            String str = "" ;
            for (int i = 0; i < integer; i++) {
                str += "a" ;
            }
            subscriber.onNext(str);
        })) ;
    }

    private Observable<List<Integer>> bufferObservable01(){
        return Observable.just(1,2,3,4,5,6,7,8,9).buffer(3) ;
    }

    private Observable<List<Integer>> bufferObservable02(){
        return Observable.<Integer>create(subscriber -> {
            for (int i = 0; i < 9; i++) {
                subscriber.onNext(i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).buffer(3, TimeUnit.SECONDS) ;
    }


    private static void print(Object obj){
        System.out.println(obj) ;
    }

}
